package com.study.javaBasic.multithreading;

import java.util.concurrent.*;

public class CallableDemo {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(4);
        // 定义任务:
        Callable<String> task = new Task();
        // 提交任务并获得Future:
        Future<String> future = executor.submit(task);
        // 从Future获取异步执行返回的结果:
        try {
//		while (!future.isDone()) {
//			System.out.println("我在等待返回结果");
//			Thread.sleep(100);
//		}
            String result = future.get();
            System.out.println(result);
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } // 可能阻塞
        executor.shutdown();
    }
}

class Task implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("返回值为String");
        Thread.sleep(1000);
        return "123";
    }

}